const app = getApp();
const { HTTP } = require('../../utils/http');
const { api } = require('../../utils/api');
const { request } = new HTTP();
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    code: ''
  },

  attached: function() {
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        app.globalData.code =  res.code;
        this.setData({
          code: res.code
        });
      }
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getPhoneNumber (e) {
      console.log('----')
      console.log(e)
      // wx.navigateTo({url: '../../pages/perfectInformation/index'});
      this._decrypt(e)
    },
    async _decrypt(e) {
      const { returnCode = '', beans = {} } = await request({
        url: api.decrypt,
        data: {
          code: this.data.code,
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        },
        method: 'post'
      });
      if (returnCode === '0000') {
        app.globalData.phoneNumber = beans.phoneNumber;
        app.globalData.appid = beans.watermark ? beans.watermark.appid : '';
        this.triggerEvent('myevent');
      }
    },

    // 暂不登录
    noLogin() {
      this.triggerEvent('myClose');
    }
  }
})